//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.bupt.cad.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bupt.cad.entity.Com;
import com.bupt.cad.entity.ComType;
import com.bupt.cad.entity.Relationship;
import com.bupt.cad.entity.RelationshipVO;
import com.bupt.cad.mapper.ComDao;
import com.bupt.cad.mapper.RelationshipDao;
import com.bupt.cad.service.IRelationshipService;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RelationshipService implements IRelationshipService {
    //@Autowired
    private RelationshipDao relationshipDao;

   // @Autowired
    private ComDao comDao;

    @Autowired
    public void setRelationshipDao(RelationshipDao relationshipDao) {
        this.relationshipDao = relationshipDao;
    }

    @Autowired
    public void setComDao(ComDao comDao) {
        this.comDao = comDao;
    }

    //需要插入两个表
    public boolean insertRelationships(JSONObject object) {
        int screenId = (Integer)object.get("screenId");
        JSONObject relationshipSettings = object.getJSONObject("relationshipSettings");
        List<Com> blueComps = relationshipSettings.getObject("blueComps", new TypeReference<List<Com>>() {});
        List<Relationship> lineList = relationshipSettings.getObject("lineList",new TypeReference<List<Relationship>>(){});
        if(screenId==0||blueComps==null||lineList==null)return false;

        comDao.deleteCompByComTypeAndScreenId(screenId, ComType.blueCom);
        relationshipDao.deleteRelationships(screenId);
        int i=1,j=1;
        if(blueComps.size()!=0){
            for(Com com:blueComps){
                com.setComType(ComType.blueCom);
                com.setScreenId(screenId);
            }
            i = comDao.insertComps(blueComps);
        }
        if(lineList.size()!=0){
            for(Relationship relationship :lineList){
                relationship.setScreenId(screenId);
            }
            j = relationshipDao.insertRelationships(lineList);
        }
        return i!=0&&j!=0;
    }

    public RelationshipVO getRelationships(int screenId) {
        List<Relationship> lineList = relationshipDao.getRelationships(screenId);
        List<Com> blueComps = comDao.getCompByComTypeAndScreenId(screenId,ComType.blueCom);
        if(lineList !=null&&blueComps!=null){
            RelationshipVO relationshipVO = new RelationshipVO();
            relationshipVO.setBlueComps(blueComps);
            relationshipVO.setLineList(lineList);
            return relationshipVO;
        }
        return null;
    }
}
